package com.li.shop.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.li.shop.domain.User;
import com.li.shop.domain.UserExample;
import com.li.shop.mapper.UserMapper;
import com.li.shop.service.VipService;
import com.li.shop.utils.SearchUserRequest;
import com.li.shop.utils.SearchUserResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class VipServiceImpl implements VipService {
    @Autowired
    private UserMapper userMapper;
    //王志文
    @Override
    //public List<User> listUser(Integer pageSize,Integer pageNum) {
    public SearchUserResponse listUser(Integer pageSize, Integer pageNum, String keyword) {
        //PageHelper.startPage(1,10);
        PageHelper.startPage(pageNum,pageSize);
        UserExample example =new UserExample();
        if (StringUtils.isNotBlank(keyword)) {
            example.createCriteria().andUserNameLike("%"+keyword+"%");
        }
        List<User> users= userMapper.selectExample2(example);
        return new SearchUserResponse(((Page) users).getTotal(), users);
        //return UserMapper.selectByExample(new UserExample());//分页后返回的page对象，page中有一个list对象
    }


    @Override
    public int deleteUserByPrimaryKey(String id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public User findUserByPrimaryKey(String id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<User> searchUsers(SearchUserRequest request) {
        UserExample ex = new UserExample();
//        ex.createCriteria().andUserIdEqualTo(request.getId());
//        ex.or().andLastNameLike("%" + request.getName() + "%");
        return userMapper.selectExample2(ex);
    }

    @Override
    public int saveUser( User user) {
        System.out.println(user.getuId()+"userid");
        if(StringUtils.isNotBlank( user.getuId()) ){
            //if( user.getuId()!=null ){
            //把domain类中的uId类型转化为String
            return userMapper.updateByPrimaryKey(user);
        } else {
            return userMapper.insert(user);
        }
    }
}
